﻿using System;
using Abp.Configuration.Startup;
using Abp.Domain.Uow;
using Microsoft.Extensions.Configuration;
using HCD.Web;
using HCD.Configuration;
using Microsoft.AspNetCore.Hosting;
using HCD.Const;

namespace HCD.EntityFrameworkCore
{
    public class MyConnectionStringResolver : DefaultConnectionStringResolver
    {
        private readonly IConfigurationRoot _appConfiguration;
        public MyConnectionStringResolver(IAbpStartupConfiguration configuration,IWebHostEnvironment hostingEnvironment) 
            : base(configuration)
        {
             _appConfiguration = AppConfigurations.Get(hostingEnvironment.ContentRootPath, hostingEnvironment.EnvironmentName);
        }

        public override string GetNameOrConnectionString(ConnectionStringResolveArgs args)
        {
            if (args["DbContextConcreteType"] as Type == typeof(HisDbContext))
            {
                return _appConfiguration.GetConnectionString(EnterpriseConsts.HisConnectionStringName);
            }
            return base.GetNameOrConnectionString(args);
        }
    }
}